﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Linq;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;

public partial class NPS_app_MasterData_CPCategoryMaster : PageBase
{


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrTitle.Text = "Master Data: Contact Person Category";
            loadView();
            setPanel();
            pnlView.Visible = true;
        }
        else
        {
            if (pnlView.Visible)
            {
                loadView();
            }
        }
    }


    private void setPanel()
    {
        pnlView.Visible = false;
        pnlCreate.Visible = false;
        pnlEdit.Visible = false;
        pnlDetails.Visible = false;
        pnlDelete.Visible = false;
    }



    protected void gv_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        loadData(e.CommandArgs.CommandArgument.ToString());
        if (e.CommandArgs.CommandName == "Editing")
        {
            goEdit();
        }
        else if (e.CommandArgs.CommandName == "Details")
        {
            goDetail();
        }
        else if (e.CommandArgs.CommandName == "Deleting")
        {
            goDelete();
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        goNew();
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        loadView();
    }


    protected void btnCSave_Click(object sender, EventArgs e)
    {
        Entities.NPSCPCategory marketingName = new Entities.NPSCPCategory()
        {
            sxCreator = ICPUser.ID,
            sxTitle = txtCTitle.Text,
            sxDetail = txtCDetail.Text
        };
        saveCPCategory(marketingName);
    }
    protected void btnCCancel_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnESave_Click(object sender, EventArgs e)
    {
        Entities.NPSCPCategory marketingName = new Entities.NPSCPCategory()
        {
            iID = getID,
            sxUpdater = ICPUser.ID,
            sxTitle = txtETitle.Text,
            sxDetail = txtEDetail.Text
        };
        updateCPCategory(marketingName);
    }
    protected void btnECancel_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDEdit_Click(object sender, EventArgs e)
    {
        goEdit();
    }
    protected void btnDBack_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Entities.NPSCPCategory marketingName = new Entities.NPSCPCategory()
        {
            iID = getID,
            sxCreator = ICPUser.ID
        };
        deleteCPCategory(marketingName);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        goView();
    }

    #region Audy's Custom Function

    protected int getID
    {
        get { return Convert.ToInt32(hfID.Value); }
    }
    protected void goView()
    {
        setPanel();
        pnlView.Visible = true;
        ltrTitle.Text = "Master Data CP Category";
        txtCDetail.Text = string.Empty;
        txtCTitle.Text = string.Empty;
    }

    protected void goEdit()
    {
        setPanel();
        pnlEdit.Visible = true;
        ltrTitle.Text = "Master Data CP Category Edit";
    }

    protected void goNew()
    {
        setPanel();
        pnlCreate.Visible = true;
        ltrTitle.Text = "Master Data CP Category Create";

    }



    protected void goDetail()
    {
        setPanel();
        pnlDetails.Visible = true;
        ltrTitle.Text = "Master Data CP Category Details";
    }

    protected void goDelete()
    {
        setPanel();
        pnlDelete.Visible = true;
        ltrTitle.Text = "Master Data CP Category Delete";
    }

    protected void loadView()
    {
        Controller.NPSCPCategoryController mnc = new Controller.NPSCPCategoryController();
        GridView1.DataSource = mnc.NPSCPCategoryList;
        GridView1.DataBind();

    }
    protected void loadData(string id)
    {
        //fetch data from DB
        Controller.NPSCPCategoryController ANController = new Controller.NPSCPCategoryController();
        Entities.NPSCPCategory CPCategory = ANController.getNPSCPCategory(Convert.ToInt32(id));

        //set all data to forms
        txtETitle.Text = CPCategory.sxTitle;
        lblDT.Text = CPCategory.sxTitle;
        lblT.Text = CPCategory.sxTitle;

        txtEDetail.Text = CPCategory.sxDetail;
        lblDD.Text = CPCategory.sxDetail;
        lblD.Text = CPCategory.sxDetail;

        lblID.Text = CPCategory.iID.ToString();
        lblDID.Text = CPCategory.iID.ToString();
        hfID.Value = CPCategory.iID.ToString();
    }
    protected void saveCPCategory(Entities.NPSCPCategory CPCategory)
    {
        CPCategory.sxCreator = ICPUser.ID;
        Controller.NPSCPCategoryController ANController = new Controller.NPSCPCategoryController();
        if (ANController.InsertNPSCPCategoryData(CPCategory) == 0)
        { //return false; 
        }
        else
        { //return true;
            loadView();
        }
        goView();
        AlertNotification("Kategori kontak person " + CPCategory.sxTitle + " telah tersimpan");
    }

    protected void updateCPCategory(Entities.NPSCPCategory CPCategory)
    {
        Controller.NPSCPCategoryController ANController = new Controller.NPSCPCategoryController();
        ANController.UpdateNPSCPCategoryData(CPCategory);
        loadView();
        goView();
        AlertNotification("Kategori kontak person " + CPCategory.sxTitle + " telah diperbaharui");
    }

    protected void deleteCPCategory(Entities.NPSCPCategory CPCategory)
    {
        Controller.NPSCPCategoryController ANController = new Controller.NPSCPCategoryController();
        ANController.DeleteNPSCPCategoryData(CPCategory);
        loadView();
        goView();
        AlertNotification("Kategori kontak person " + CPCategory.sxTitle + " telah dihapus");
    }
    #endregion
}
